package 简单.N叉树的前序遍历;

import java.util.*;

//迭代
public class Solution2 {
    public List<Integer> preorder(Node root) {

        ArrayList<Integer> ans = new ArrayList<>();
        LinkedList<Node> stack = new LinkedList<>();

        if(root == null) return ans;
        stack.push(root);
        while (!stack.isEmpty()){
            Node tmp = stack.pollLast();
            ans.add(tmp.val);
            Collections.reverse(tmp.children);
            for (Node child : tmp.children) {
                stack.add(child);
            }
        }
        return ans;
    }
}
